package us.icitap.cm.utils;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import us.icitap.timsportal.business.SimpleUser;
import us.icitap.timsportal.ejb.remote.PortalBeanRemote;
import us.icitap.timsportal.entity.PortalSession;
import us.icitap.timsportal.entity.User;

public class TPUtil {
	
	public static final String PORTAL_SESSION_REF = "portalSessionId";
	public static final String MODULE_SESSION_REF = "moduleSessionId";
	public static final String USER = "user";
	
	public static final String PORTAL_LOCATION = "http://192.168.3.133:8080/TimsPortalWEB/";
	
	public static PortalBeanRemote portalBeanRemote;
	
	private HttpServletRequest httpRequest;
	private HttpSession httpSession;
	
	private static Properties properties;
	
	static {
		properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
		//result.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
		//result.setProperty("java.naming.factory.state",	"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
		properties.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.3.133");
		properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
	}
	
	static {		
		InitialContext init;
		try {

			init = new InitialContext(properties);
			portalBeanRemote = (PortalBeanRemote) init.lookup("java:global/TimsPortalEAR/TimsPortalEJB/PortalBean!us.icitap.timsportal.ejb.remote.PortalBeanRemote");	
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public TPUtil(HttpServletRequest httpRequest){
		this.httpRequest = httpRequest;
		this.httpSession = httpRequest.getSession();
	}
	
	public boolean checkRequestParams(){
		boolean isValid = true;
		String portalSessionId = null;
		boolean noQueryString = httpRequest.getParameter(PORTAL_SESSION_REF) == null;
		if(noQueryString)
			return isValid = false;
		portalSessionId = httpRequest.getParameter("portalSessionId");
		if(portalSessionId == null || portalSessionId.equals(""))
			return isValid = false;
		else return isValid;

	}
	
	public boolean isValidPortalEntry(){
		boolean isValid = true;
		String portalSessionId = httpRequest.getParameter(PORTAL_SESSION_REF);
		PortalSession portalSession = portalBeanRemote
										.searchPortalSessionInDB(portalSessionId);
		if(portalSession==null)return isValid = false;
		if(portalSession.getModuleEntrySuccess()!=null 
				&& portalSession.getModuleEntrySuccess()==true)
			return isValid = false;
		return isValid;
	}
	
	public boolean loadUserOnSession(){
		boolean isValid = true;
		String portalSessionId = httpRequest.getParameter(PORTAL_SESSION_REF);
		String moduleSessionId = httpRequest.getSession(true).getId();
		
		SimpleUser user = portalBeanRemote.getModuleUser(portalSessionId,
				moduleSessionId);
		if(user==null)
			isValid = false;

		httpRequest.getSession().setAttribute(PORTAL_SESSION_REF, portalSessionId);
		httpRequest.getSession().setAttribute(MODULE_SESSION_REF, moduleSessionId);
		httpRequest.getSession().setAttribute(USER, user);
		
		return isValid;
	}
	
	public String getReturnURL(){
		String url = PORTAL_LOCATION 
		+ "?" +	PORTAL_SESSION_REF +"="
		+ httpSession.getAttribute(PORTAL_SESSION_REF).toString()
		+ "&" + MODULE_SESSION_REF +"="
		+ httpSession.getAttribute(MODULE_SESSION_REF).toString();
		
		return url;
	}

}
